#pragma once
#ifndef MYTRIANGLE_H
#define MYTRIANGLE_H

#include "MyPrimitive.h"
#include "MyEmissiveMaterial.h"
#include <math.h>
#include <stdlib.h>

class MyTriangle : public MyPrimitive {

public:

	~MyTriangle(void){}
	MyTriangle(MyMaterial mat, MyVector3 one, MyVector3 two, MyVector3 three){
	
		this->one = one;
		this->two = two;
		this->three = three;
		this->m = mat;
		this->setNormal();

	}

	MyTriangle(MyEmissiveMaterial mat, MyVector3 one, MyVector3 two, MyVector3 three){
	
		this->one = one;
		this->two = two;
		this->three = three;
		this->eMat = mat;
		this->setNormal();

	}

	MyVector3 one;
	MyVector3 two;
	MyVector3 three;
	MyVector3 normal;
	MyEmissiveMaterial eMat;
		
	MyVector3 getNormal(MyVector3 point);
	void setNormal();
	int whatIsIt();
	bool isIntersection(MyRay ray, float &t); 
	MyVector3 getRandomPoint();
	float getArea();
};

int MyTriangle::whatIsIt(){

	return 1;
}

void MyTriangle::setNormal(){

	MyVector3 u = this->one.minus(this->two);
	MyVector3 v = this->three.minus(this->two);
	
	this->normal = v.vectorMult(u);
	this->normal.normalize();
}
MyVector3 MyTriangle::getNormal(MyVector3 point){

	return this->normal;
}


bool MyTriangle::isIntersection(MyRay ray, float &t){

	MyVector3 e1 = this->two.minus(this->one);
	MyVector3 e2 = this->three.minus(this->one);
	
	MyVector3 s1 = ray.direction.vectorMult(e2);
//	printf("s1: %f %f %f\n", s1.x, s1.y, s1.z);
	float divisor = s1.scalarMult(e1);
	
	if (divisor < 0.0001 && divisor > -0.0001)
		return false;

	float invDivisor = 1.f / divisor;

	MyVector3 d = ray.origin.minus(this->one);
	
	float b1 = d.scalarMult(s1) * invDivisor;
	
	if (b1 < 0.0 || b1 > 1.0)
		return false;
	
	MyVector3 s2 = d.vectorMult(e1);
		
	float b2 = s2.scalarMult(ray.direction) * invDivisor;
	
	if (b2 < 0.0 || b1 + b2 > 1.0)
		return false;	
	
	t = e2.scalarMult(s2) * invDivisor;
	//printf("Triangle t=%f\n", t);
	if (t < 0 || ray.direction.scalarMult(normal) >= 0) return false;
	
	return true;
}

MyVector3 MyTriangle::getRandomPoint(){

	float randomNumber1 = (float)rand()/(float)RAND_MAX;
	float randomNumber2 = (float)rand()/(float)RAND_MAX;
	
	while(randomNumber1 + randomNumber2 > 1){
		randomNumber1 = (float)rand()/(float)RAND_MAX;
		randomNumber2 = (float)rand()/(float)RAND_MAX;
	}
	/*
	if(randomNumber1 + randomNumber2 > 1){
	
		randomNumber1 = 1 - randomNumber1;
		randomNumber2 = 1 - randomNumber2;
	}
	*/
	//printf("%f %f \n",randomNumber1,randomNumber2);
	MyVector3 v = this->three.minus(this->one);
	MyVector3 u = this->two.minus(this->one);
	

	v = v.constantMult(randomNumber1);
	u = u.constantMult(randomNumber2);
	
	return v.plus(u).plus(this->one);
}

float MyTriangle::getArea(){

	float sizeA = this->three.minus(this->one).size();
	float sizeB = this->two.minus(this->one).size();
	float sizeC = this->three.minus(this->two).size();

	float s = (sizeA + sizeB + sizeC) / 2.0f;

	return sqrtf(s*(s-sizeA)*(s-sizeB)*(s-sizeC));
}

#endif